package com.rongxun.api;

import android.content.Context;

import com.rongxun.api.dao.response.UserInfo;
import com.rongxun.api.utils.PreferenceUtils;

/**
 * 创建者     庄丰泽
 * 创建时间   2020/5/22 10:17
 * 描述
 * <p>
 * 更新者     $
 * 更新时间   $
 * 更新描述
 */
public class UserInfoManager {


    private static final String TOKEN                     = "token";
    private static final String ID_CARD_STATUS            = "id_card_status";
    private static final String ID                        = "id";
    private static final String USERNAME                  = "username";
    private static final String MT4_ID                    = "mt4_id";
    private static final String EMAIL                     = "email";
    private static final String PHONE                     = "phone";
    private static final String BANK_STATUS               = "bank_status";
    private static final String NICKNAME                  = "nickname";
    private static final String FACE_IMG                  = "face_img";
    private static final String REG_TIME                  = "reg_time";
    private static final String UPLOADDATA                = "uploaddata";
    private static final String LIRUN                     = "lirun";
    private static final String SURNAMES                  = "surnames";
    private static final String NAME                      = "name";
    private static final String SERVERS_URL               = "servers_url";
    private static final String WEBSOCKET_URL             = "WEBSOCKET_URL";
    private static final String IS_DEMOMT4ID              = "is_demoMt4id";
    private static final String IS_LIVEMT4ID              = "is_LiveMt4id";
    private static final String USER_TYPE                 = "user_type";
    private static final String LIVE_URL                  = "LIVE_URL";
    private static final String FIRST_LAUNCH              = "FIRST_LAUNCH";
    private static final String PC_TOKEN                  = "PC_TOKEN";
    private static final String ACTIVITY_TYPE             = "ACTIVITY_TYPE";
    private static final String UI_CONFIG                 = "UI_CONFIG";
    private static final String ADVERTISEMENT_IMAGE       = "advertisement_image";
    private static final String USERLOGINTIME             = "userLoginTime";
    private static final String LOGINIMAGE                = "loginImage";
    private static final String UPSANDDOWNS               = "upsAndDowns";
    private static final String IS_FIRST_OPEN             = "isFirstOpen";
    private static final String IS_FIRST_OPEN_ONEKEYCLOSE = "is_onekeyclose";


    private String token;
    private int    id_card_status;
    private String id;
    private String username;
    private String mt4_id;
    private String email;
    private String phone;
    private int    bank_status;

    private String  nickname;
    private String  face_img;
    private String  reg_time;
    private String  uploaddata;
    private String  lirun;
    private String  name;
    private String  surnames;
    private String  servers_url;
    private String  websocket_url;
    private int     is_LiveMt4id;
    private String  user_type;
    private String  Live_url;
    private int     is_demoMt4id;
    private boolean first_launch;
    private String  pc_token;
    private String  activityType;
    private String  advertisement_image;
    private String  userLoginTime;
    private String  loginImage;
    private String  upsAndDowns;
    private boolean isFirstOpen;


    private boolean isFirstOpenOneKeyClose;

    private static volatile UserInfoManager mInstance;


    private String UIConfig;

    public static final String NO_TRANSACTION   = "1";//A面
    public static final String NEED_TRANSACTION = "2";//B面


    public static UserInfoManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (UserInfoManager.class) {
                if (mInstance == null) {
                    mInstance = new UserInfoManager();
                }
            }
        }
        return mInstance;
    }


    private UserInfoManager() {

        UIConfig = PreferenceUtils.getString(UI_CONFIG, NO_TRANSACTION);


        token = PreferenceUtils.getString(TOKEN, "");
        id_card_status = PreferenceUtils.getInt(ID_CARD_STATUS, 0);

        id = PreferenceUtils.getString(ID, "");
        username = PreferenceUtils.getString(USERNAME, "");
        mt4_id = PreferenceUtils.getString(MT4_ID, "");
        email = PreferenceUtils.getString(EMAIL, "");
        phone = PreferenceUtils.getString(PHONE, "");
        bank_status = PreferenceUtils.getInt(BANK_STATUS, 0);
        nickname = PreferenceUtils.getString(NICKNAME, "");
        face_img = PreferenceUtils.getString(FACE_IMG, "");
        reg_time = PreferenceUtils.getString(REG_TIME, "");
        uploaddata = PreferenceUtils.getString(UPLOADDATA, "");
        lirun = PreferenceUtils.getString(LIRUN, "0");

        name = PreferenceUtils.getString(NAME, "");
        surnames = PreferenceUtils.getString(SURNAMES, "");
        servers_url = PreferenceUtils.getString(SERVERS_URL, "");
        websocket_url = PreferenceUtils.getString(WEBSOCKET_URL, "");
        is_demoMt4id = PreferenceUtils.getInt(IS_DEMOMT4ID, 0);
        is_LiveMt4id = PreferenceUtils.getInt(IS_LIVEMT4ID, 0);
        user_type = PreferenceUtils.getString(user_type, "");
        Live_url = PreferenceUtils.getString(LIVE_URL, "");
        first_launch = PreferenceUtils.getBoolean(FIRST_LAUNCH, true);
        pc_token = PreferenceUtils.getString(PC_TOKEN, "");
        activityType = PreferenceUtils.getString(ACTIVITY_TYPE, "");
        advertisement_image = PreferenceUtils.getString(ADVERTISEMENT_IMAGE, "");
        userLoginTime = PreferenceUtils.getString(USERLOGINTIME, "");
        loginImage = PreferenceUtils.getString(LOGINIMAGE, "");
        upsAndDowns = PreferenceUtils.getString(UPSANDDOWNS, "红涨绿跌");

        isFirstOpen = PreferenceUtils.getBoolean(IS_FIRST_OPEN, true);

        isFirstOpenOneKeyClose = PreferenceUtils.getBoolean(IS_FIRST_OPEN_ONEKEYCLOSE, true);
    }


    public void saveUserInfoToSP(Context context, UserInfo response) {
        setId_card_status(response.getId_card_status());
        setId(response.getId());
        setUsername(response.getUsername());
        if (UserInfoManager.getInstance(context).getMt4_id() != null && response.getMt4_id() != 0)
            setMt4_id(String.valueOf(response.getMt4_id()));
        setEmail(response.getEmail());
        setPhone(response.getPhone());
        setBank_status(response.getBank_status());
        setNickname(response.getNickname());
        setFace_img(response.getFace_img());
        setReg_time(response.getReg_time());
        setSurnames(response.getSurnames());
        setName(response.getName());
        if (response.getToken() != null && !response.getToken().isEmpty())
            setToken(response.getToken());
        setIs_demoMt4id(response.getIs_demoMt4id());
        setIs_LiveMt4id(response.getIs_LiveMt4id());
        setUser_type(response.getUser_type());
        setLive_url(response.getLive_url());
        setServers_url(response.getServers_url());
        setWebsocket_url(response.getWebsocket_url());
        setPc_token(response.getPc_token());

        setAgent_mode(response.getAgent_mode());
    }

    private String agent_mode;

    public String getAgent_mode() {
        return agent_mode;
    }

    public void setAgent_mode(String agent_mode) {
        this.agent_mode = agent_mode;
    }

    public boolean isFirstOpen() {
        return isFirstOpen;
    }

    public void setFirstOpen(boolean firstOpen) {
        isFirstOpen = firstOpen;
        PreferenceUtils.putBoolean(IS_FIRST_OPEN, firstOpen);
    }


    public boolean isFirstOpenOneKeyClose() {
        return isFirstOpenOneKeyClose;
    }

    public void setIsFirstOpenOnekeyclose(boolean isFirstOpenOneKeyClose) {
        this.isFirstOpenOneKeyClose = isFirstOpenOneKeyClose;
        PreferenceUtils.putBoolean(IS_FIRST_OPEN, isFirstOpenOneKeyClose);
    }


    public String getUpsAndDowns() {
        return upsAndDowns;
    }

    public void setUpsAndDowns(String upsAndDowns) {
        this.upsAndDowns = upsAndDowns;
        PreferenceUtils.putString(UPSANDDOWNS, upsAndDowns);
    }

    public String getLoginImage() {
        return loginImage;
    }

    public void setLoginImage(String loginImage) {
        this.loginImage = loginImage;
        PreferenceUtils.putString(LOGINIMAGE, loginImage);
    }


    public String getUserLoginTime() {
        return userLoginTime;
    }

    public void setUserLoginTime(String userLoginTime) {
        this.userLoginTime = userLoginTime;
        PreferenceUtils.putString(USERLOGINTIME, userLoginTime);
    }


    public String getAdvertisement_image() {
        return advertisement_image;
    }

    public void setAdvertisement_image(String advertisement_image) {
        this.advertisement_image = advertisement_image;
        PreferenceUtils.putString(ADVERTISEMENT_IMAGE, advertisement_image);
    }

    public String getUIConfig() {
        return UIConfig;
    }

    public void setUIConfig(String UIConfig) {
        this.UIConfig = UIConfig;
        PreferenceUtils.putString(UI_CONFIG, UIConfig);
    }

    public String getActivityType() {
        return activityType;
    }

    public void setActivityType(String activityType) {
        this.activityType = activityType;
        PreferenceUtils.putString(ACTIVITY_TYPE, activityType);
    }

    public String getPc_token() {
        return pc_token;
    }

    public void setPc_token(String pc_token) {
        this.pc_token = pc_token;
        PreferenceUtils.putString(PC_TOKEN, pc_token);
    }

    public boolean isFirst_launch() {
        return first_launch;
    }

    public void setFirst_launch(boolean lanch) {
        this.first_launch = lanch;
        PreferenceUtils.putBoolean(FIRST_LAUNCH, first_launch);
    }

    public String getLive_url() {
        if (Live_url == null || Live_url.isEmpty()) {
            //            return ApiConstants.DAILY_URL;
        } else {
            return Live_url;
        }

        return null;
    }

    public void setLive_url(String live_url) {
        Live_url = live_url;
        PreferenceUtils.putString(LIVE_URL, live_url);
    }

    public void setUser_type(String user_type) {
        this.user_type = user_type;
        PreferenceUtils.putString(USER_TYPE, user_type);
    }

    public String getUser_type() {
        return user_type;
    }


    public int getIs_LiveMt4id() {
        return is_LiveMt4id;
    }

    public void setIs_LiveMt4id(int is_LiveMt4id) {
        this.is_LiveMt4id = is_LiveMt4id;
        PreferenceUtils.putInt(IS_LIVEMT4ID, is_LiveMt4id);
    }

    public int getIs_demoMt4id() {
        return is_demoMt4id;
    }

    public void setIs_demoMt4id(int is_demoMt4id) {
        this.is_demoMt4id = is_demoMt4id;
        PreferenceUtils.putInt(IS_DEMOMT4ID, is_demoMt4id);
    }

    public String getWebsocket_url() {
        return websocket_url;
    }

    public void setWebsocket_url(String websocket_url) {
        this.websocket_url = websocket_url;
        PreferenceUtils.putString(WEBSOCKET_URL, websocket_url);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        PreferenceUtils.putString(NAME, name);
    }


    public String getServers_url() {
        return servers_url;

    }

    public void setServers_url(String servers_url) {
        this.servers_url = servers_url;
        PreferenceUtils.putString(SERVERS_URL, servers_url);
    }


    public String getSurnames() {
        return surnames;
    }

    public void setSurnames(String surnames) {
        this.surnames = surnames;
        PreferenceUtils.putString(SURNAMES, surnames);
    }

    public String getLirun() {
        return lirun;
    }

    public void setLirun(String lirun) {
        this.lirun = lirun;
        PreferenceUtils.putString(LIRUN, lirun);
        //        RxBus.get().post(new Events.upLirunData());
    }

    public String getUploaddata() {
        return uploaddata;
    }

    public void setUploaddata(String uploaddate) {
        this.uploaddata = uploaddate;
        PreferenceUtils.putString(UPLOADDATA, uploaddate);
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
        PreferenceUtils.putString(NICKNAME, nickname);
    }

    public String getFace_img() {
        return face_img;
    }

    public void setFace_img(String face_img) {
        this.face_img = face_img;
        PreferenceUtils.putString(FACE_IMG, face_img);
    }

    public String getReg_time() {
        return reg_time;
    }

    public void setReg_time(String reg_time) {
        this.reg_time = reg_time;
        PreferenceUtils.putString(REG_TIME, reg_time);
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
        PreferenceUtils.putString(TOKEN, token);
    }


    public int getId_card_status() {
        return id_card_status;
    }

    public void setId_card_status(int id_card_status) {
        this.id_card_status = id_card_status;
        PreferenceUtils.putInt(ID_CARD_STATUS, id_card_status);
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
        //        App.getInstance().getSession().setUserId(User.get().getId());
        PreferenceUtils.putString(ID, id);
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
        PreferenceUtils.putString(USERNAME, username);
    }

    public String getMt4_id() {
        if (mt4_id.equals("0")) {
            return "";
        }
        return mt4_id;
    }

    public void setMt4_id(String mt4_id) {
        this.mt4_id = mt4_id;
        PreferenceUtils.putString(MT4_ID, mt4_id);
        //        Events.SetMt4Id setMt4Id = new Events.SetMt4Id();
        //        RxBus.get().post(setMt4Id);
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
        PreferenceUtils.putString(EMAIL, email);
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
        PreferenceUtils.putString(PHONE, phone);
    }

    public int getBank_status() {
        return bank_status;
    }

    public void setBank_status(int bank_status) {
        this.bank_status = bank_status;
        PreferenceUtils.putInt(BANK_STATUS, bank_status);
    }

    public void clear() {

        PreferenceUtils.reset();

        //        PreferenceUtils.remove(TOKEN);
        //        token = "";
        //
        //        PreferenceUtils.remove(MT4_ID);
        //        mt4_id = "";
        //
        //        PreferenceUtils.remove(LIRUN);
        //        lirun = "";
        //
        //        PreferenceUtils.remove(ID);
        //        id = "";

        //        App.getInstance().getSession().setUserId("");
        //
        //        User.get().setServers_url(ApiConstants.DAILY_URL_CEO);
    }
}

